var ShopList = cc.Class({
  extends: cc.Component,

  properties: () => ({
    MoneyLab: {
      default: null,
      type: cc.Label
    },
    tiliLab: {
      default: null,
      type: cc.Label
    },
    zhengqiLab: {
      default: null,
      type: cc.Label
    },
    batiLab: {
      default: null,
      type: cc.Label
    },
  }),
  onLoad: function() {
    // 单价
    this.tiliPrice = 10;
    this.zhengqiPrice = 10;
    this.batiPrice = 10;
    // 云
    wx.cloud.init({
      traceUser: true
    });
    // 云函数 获取 openid或者appid
    wx.cloud.callFunction({
        name: 'ganjiangmoye',
        complete: res => {
            var OPENID = res.result.userInfo.openId;
            this.getData(OPENID);
            console.log('云端获取用户数据: ',res.result.userInfo, OPENID);
        }
    });
  },
  // 云端获取用户数据
  getData: function(OPENID) {
    var _this = this;
    console.log("getdata", OPENID);
    const db = wx.cloud.database();
    db.collection('ganjiangmoye').where({
        _openid: OPENID,
    })
    .get({
        success: function(res) {
          // res.data 是一个包含集合中有权限访问的所有记录的数据，不超过 20 条
          if(res.data.length == 0) {
            _this.addData();
            _this.totalMoney = 0;
            _this.tiliNum = 10;
            _this.zhengqiNum = 10;
            _this.batiNum = 0;
            _this.MoneyLab.string = '金额: 0';
            _this.tiliLab.string = '还可以购买10次';
            _this.zhengqiLab.string = '还可以购买10次';
            _this.batiLab.string = '已持有0个';
            console.log("getData_success_get长度为0", res.data);
          }else {
            _this._id = res.data[0]._id;
            _this.totalMoney = res.data[0].totalMoney;
            _this.tili = res.data[0].tili;
            _this.tiliNum = res.data[0].tiliNum;
            _this.zhengqi = res.data[0].zhengqi;
            _this.zhengqiNum = res.data[0].zhengqiNum;
            _this.batiNum = res.data[0].batiNum;
            _this.MoneyLab.string = '金额:' + _this.totalMoney;
            _this.tiliLab.string = '还可以购买' + _this.tiliNum + '次';
            _this.zhengqiLab.string = '还可以购买' + _this.zhengqiNum + '次';
            _this.batiLab.string = '已持有' + _this.batiNum + '个';
            console.log("getData_success_get长度不为0", res.data);
          }
        },
        fail: function(res) {
            console.log("getData_fail_get", res);
        },
        complete: function(res) {
            console.log("getData_complete_get222", res);
        } 
    })
  },
  // 初始化云开发添加数据
  addData: function() {
    // 初始给云数据添加
    const db = wx.cloud.database();
    db.collection('ganjiangmoye').add({
        // data 字段表示需新增的 JSON 数据
        data: {
          totalMoney: 0,
          maxScore: 0,
          tili: 20,
          zhengqi: 20,
          tiliNum: 10,
          zhengqiNum: 10,
          batiNum: 0,
        },
        success: function(res) {
            // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
            console.log('addData_success_add', res);
        },
        fail: function(res) {
            console.log("addData_fail_add", res);
        },
        complete: function(res) {
            console.log("addData_complete_add", res);
        } 
    })
  },
  // 购买体力 上限10次
  buyTili: function() {
    if(this.tiliNum <= 0) return;
    if(this.totalMoney >= this.tiliPrice && this.tiliNum != 0) {
      this.totalMoney -= this.tiliPrice;
      this.MoneyLab.string = '金额:' + this.totalMoney.toString();
      this.tiliNum -= 1;
      this.tiliLab.string = '还可以购买' + this.tiliNum.toString() + '次';
      this.tili += 5;
      const db = wx.cloud.database();
      db.collection('ganjiangmoye').doc(this._id).update({
          data: {
            totalMoney: this.totalMoney,
            tili: this.tili,
            tiliNum: this.tiliNum
          }
      })
    }else return
    console.log(this.MoneyLab.string,this.tiliLab.string, '金币和体力数');
  },
  // 购买真气 上限10次
  buyZhenqi: function() {
    if(this.zhengqiNum <= 0) return;
    if(this.totalMoney >= this.zhengqiPrice && this.zhengqiNum != 0) {
      this.totalMoney -= this.zhengqiPrice;
      this.MoneyLab.string = '金额:' + this.totalMoney.toString();
      this.zhengqiNum -= 1;
      this.zhengqiLab.string = '还可以购买' + this.zhengqiNum.toString() + '次';
      this.zhengqi -= 1;
      const db = wx.cloud.database();
      db.collection('ganjiangmoye').doc(this._id).update({
          data: {
            totalMoney: this.totalMoney,
            zhengqi: this.zhengqi,
            zhengqiNum: this.zhengqiNum
          }
      })
    }else return
    console.log(this.MoneyLab.string,this.zhengqiLab.string, '金币和真气数');
  },
  // 购买霸体 无限
  buyBati: function() {
    if(this.totalMoney >= this.batiPrice) {
      this.totalMoney -= this.batiPrice;
      this.MoneyLab.string = '金额:' + this.totalMoney.toString();
      this.batiNum += 1;
      this.batiLab.string = '已持有' + this.batiNum.toString() + '个';
      const db = wx.cloud.database();
      db.collection('ganjiangmoye').doc(this._id).update({
        data: {
          totalMoney: this.totalMoney,
          batiNum: this.batiNum
        }
      })
    }else return
    console.log(this.MoneyLab.string,this.batiLab.string, '金币和霸体数');
  },
});
module.exports = ShopList;